package com.css.dzj.dzjap.modules.enterprise.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjap.common.util.BeanUtil;
import com.css.dzj.dzjap.common.util.Constant;
import com.css.dzj.dzjap.common.util.HttpClientUtil;
import com.css.dzj.dzjap.common.util.R;
import com.css.dzj.dzjap.modules.enterprise.entity.ApEnterpriseCreditEntity;
import com.css.dzj.dzjap.modules.enterprise.service.EnterpriseService;
import com.css.dzj.dzjap.modules.publick.entity.ApPublicEntity;
import com.css.dzj.dzjap.modules.publick.service.PublicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/enterPrise")
public class EnterpriseController {

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private PublicService publicService;

    @Value("${ZFJG_URL}")
    private String zfjgUrl;

    @Autowired
    BaseService baseService;
    /**
     * 查询评价单位或建设单位信用信息表
     */
    @GetMapping(value = "/getEnterPrise")
    public R getEnterPrise(@RequestParam("enterpriseCreditId") String enterpriseCreditId,
                           @RequestParam("enterpriseCredittype") String enterpriseCredittype){
        baseService.logSuccess(3,"查询成功","2");
      return R.ok().put("data",enterpriseService.getEnterPrise(enterpriseCreditId,enterpriseCredittype));
    }



    /**
     * 添加评价单位或建设单位信用信息表
     */
    @PostMapping(value = "/addEnterPrise")
    public R addEnterPrise(@RequestBody ApEnterpriseCreditEntity enterPrise) {

        if (StringUtils.isEmpty(enterPrise.getEnterpriseName()) || StringUtils.isEmpty(enterPrise.getEnterpriseIdNumber())
                || StringUtils.isEmpty(enterPrise.getEnterpriseScore()) || StringUtils.isEmpty(enterPrise.getEnterpriseClassification()) || enterPrise.getAssessmentDate() == null
                || StringUtils.isEmpty(enterPrise.getAssessmentAuthority()) || StringUtils.isEmpty(enterPrise.getEnterpriseScoreNumber())) {

            return R.error("必填字段为空");
        }
        try {
            String flag = enterpriseService.addEnterPrise(enterPrise);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"添加成功","2");
                return r;
            } else {
                baseService.logFailed(3,"添加失败","2");
                return R.error("操作失败");
            }
        }catch (Exception e){
            return R.error(e.getMessage());
        }

    }

    /**
     * 修改评价单位或建设单位信用信息表
     */
    @PostMapping(value = "/updateEnterPrise")
    public R updateEnterPrise(@RequestBody ApEnterpriseCreditEntity enterPrise ){
        System.out.println(enterPrise.getEnterpriseName());
        try {

            if(!StringUtils.isEmpty(enterPrise) && !StringUtils.isEmpty(enterPrise.getCreditClueReasons())) {
                if("信用分类D".equals(enterPrise.getCreditClueReasons()) || "信用分类E".equals(enterPrise.getCreditClueReasons()) ||
                        "信用分类一次性下降≥2档".equals(enterPrise.getCreditClueReasons()) || "击中特定负面".equals(enterPrise.getCreditClueReasons())){
                    String enterpriseVO = JSON.toJSONStringWithDateFormat(enterPrise,"yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
                    String result = HttpClientUtil.doPostJson(zfjgUrl + "/clue/enterprisevo",enterpriseVO);
                }
            } else {
                return R.error("企业信用信息或信用线索必填字段为空");
            }

            ApEnterpriseCreditEntity ePrise = enterpriseService.getEnterPrise(enterPrise.getEnterpriseCreditId(),enterPrise.getEnterpriseCredittype());
            BeanUtil.copyBeanNotNullToBean(enterPrise, ePrise);
            ePrise.setUpdateTime(new Date());
            enterpriseService.update(ePrise);
            baseService.logSuccess(3,"修改成功","2");
            return R.ok("操作成功！");
        } catch (Exception e) {
            baseService.logFailed(3,"修改失败","2");
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除评价单位或建设单位信用信息表
     */
    @PostMapping(value = "/deleteEnterPrise")
    public R deleteEnterPrise(@RequestParam("enterpriseCreditId")  String enterpriseCreditId,
                              @RequestParam("enterpriseCredittype")  String enterpriseCredittype){
        System.out.println(enterpriseCreditId);
        if(enterpriseService.deleteEnterPrise(enterpriseCreditId,enterpriseCredittype)){
            baseService.logSuccess(3,"删除成功","2");
            return R.ok("操作成功!");
        }else{
            baseService.logFailed(3,"删除失败","2");
            return  R.error("操作失败!");
        }
    }


    /**
     *移入评价单位或建设单位信用信息表进入黑名单
     */
    @PostMapping(value = "/moveinEnterPrise")
    public R moveinEnterPrise(@RequestBody ApEnterpriseCreditEntity enterPrise){
          if(StringUtils.isEmpty(enterPrise.getSpecause())){
            return R.error("必填字段不能为空！");
          }
        try {

            ApPublicEntity pub = publicService.getGlTYPublick(enterPrise.getEnterpriseCreditId(),enterPrise.getEnterpriseCredittype());
            if(pub!=null){
                ApPublicEntity aPub = new ApPublicEntity();
                pub.setPublicUrl("/dzjap/enterPrise/getReEnterPrise");
                BeanUtil.copyBeanNotNullToBean(aPub, pub);
                publicService.update(aPub);
            }


            ApEnterpriseCreditEntity ePrise = enterpriseService.getEnterPrise(enterPrise.getEnterpriseCreditId(),enterPrise.getEnterpriseCredittype());
            BeanUtil.copyBeanNotNullToBean(enterPrise, ePrise);
            ePrise.setExcpresState(Constant.EXCPRES_STATE_Yes);
            ePrise.setAbnormalPutdate(new Date());
            enterpriseService.update(ePrise);
            baseService.logSuccess(3,"移入成功","2");
            return R.ok("操作成功！");
        } catch (Exception e) {
            baseService.logFailed(3,"移入失败","2");
            return R.error(e.getMessage());
        }
    }


    /**
     *移出评价单位或建设单位信用信息表进入黑名单
     */
     @PostMapping(value = "/moveoutEnterPrise")
     public R moveoutEnterPrise(@RequestBody ApEnterpriseCreditEntity enterPrise){
         if(StringUtils.isEmpty(enterPrise.getRemexcpres())){
             return R.error("必填字段不能为空！");
         }
         try {

             ApPublicEntity pub = publicService.getGlTYPublick(enterPrise.getEnterpriseCreditId(),enterPrise.getEnterpriseCredittype());
             if(pub!=null){
                 ApPublicEntity aPub = new ApPublicEntity();
                 pub.setPublicUrl("/dzjap/enterPrise/getEnterPrise");
                 BeanUtil.copyBeanNotNullToBean(aPub, pub);
                 publicService.update(aPub);
             }

             ApEnterpriseCreditEntity ePrise = enterpriseService.getReEnterPrise(enterPrise.getEnterpriseCreditId(),enterPrise.getEnterpriseCredittype());
             BeanUtil.copyBeanNotNullToBean(enterPrise, ePrise);
             ePrise.setExcpresState(Constant.EXCPRES_STATE_NO);
             ePrise.setRemdate(new Date());
             enterpriseService.update(ePrise);
             baseService.logSuccess(3,"移出成功","2");
             return R.ok("操作成功！");
         } catch (Exception e) {
             baseService.logFailed(3,"移出失败","2");
             return R.error(e.getMessage());
         }
     }


    /**
     * 查询评价单位或建设单位信用信息表黑名单
     */
    @GetMapping(value = "/getReEnterPrise")
    public R getReEnterPrise(@RequestParam("enterpriseCreditId") String enterpriseCreditId,
                           @RequestParam("enterpriseCredittype") String enterpriseCredittype){
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data",enterpriseService.getReEnterPrise(enterpriseCreditId,enterpriseCredittype));
    }


    /**
     *分页查询所有评价单位或建设单位信用信息表
     */
    @GetMapping(value = "/getAllEnterPrise")
    public R getAllEnterPrise(@RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize,
                              @RequestParam("enterpriseCredittype") String enterpriseCredittype, HttpServletRequest request) {

        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", enterpriseService.getAllEnterPrise(pageRequest,enterpriseCredittype,request));
    }


    /**
     *分页查询所有评价单位或建设单位信用信息表黑名单
     */
    @GetMapping(value = "/getAllReEnterPrise")
    public R getAllReEnterPrise(@RequestParam(name = "pageNo" ,required = false) Integer pageNo,
                              @RequestParam(name = "pageSize" ,required = false) Integer pageSize,
                              @RequestParam("enterpriseCredittype")  String enterpriseCredittype,
                                HttpServletRequest request) {
        System.out.println(pageNo);
        System.out.println(pageSize);
        System.out.println(enterpriseCredittype);
        PageRequest pageRequest = PageRequest.of(pageNo-1, pageSize);
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", enterpriseService.getAllReEnterPrise(pageRequest,enterpriseCredittype,request));
    }


    /**
     *多条件分页查询所有评价单位或建设单位信用信息表
     */
    @GetMapping(value ="/getAllConEnterPrise")
    public R getAllConEnterPrise(@RequestParam(required = false) String enterpriseName,
                                 @RequestParam String enterpriseCredittype,
                                 @RequestParam String enterpriseScoreNumber,
                                 @RequestParam String enterpriseIdNumber,
                                 @RequestParam String enterpriseScore,
                                 @RequestParam String enterpriseClassification,
                                 @RequestParam String organName,
                                 @RequestParam(required = false) Integer pageNo,
                                 @RequestParam(required = false) Integer pageSize,
                                 HttpServletRequest request){

        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        /*Timestamp begin = (Timestamp)beginTime;
        Timestamp end = (Timestamp)endTime;*/
        baseService.logSuccess(3,"查询成功","2");
          return R.ok().put("data", enterpriseService.getAllConEnterPrise(pageRequest,enterpriseCredittype,enterpriseName,
                  enterpriseScoreNumber,enterpriseIdNumber,enterpriseScore,enterpriseClassification,organName,request));
    }

    /**
     *多条件分页查询所有评价单位或建设单位信用信息表黑名单
     */
    @GetMapping(value ="/getAllReConEnterPrise")
    public R getAllReConEnterPrise(@RequestParam(required = false) String enterpriseName,
                                   @RequestParam  String enterpriseCredittype,
                                   @RequestParam String enterpriseScoreNumber,
                                   @RequestParam String enterpriseIdNumber,
                                   @RequestParam String enterpriseIdType,
                                   @RequestParam String enterpriseScore,
                                   @RequestParam String enterpriseClassification,
                                   @RequestParam(required = false) Integer pageNo,
                                   @RequestParam(required = false) Integer pageSize,
                                   HttpServletRequest request){



        PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
        /*Timestamp begin = (Timestamp)beginTime;
        Timestamp end = (Timestamp)endTime;*/
        baseService.logSuccess(3,"查询成功","2");
        return R.ok().put("data", enterpriseService.getAllReConEnterPrise(pageRequest,enterpriseCredittype,enterpriseName,
                   enterpriseScoreNumber,enterpriseIdNumber,enterpriseScore,enterpriseClassification,request));
    }

    /**
     * 通过统一信用代码查询评价单位或建设单位信用信息黑名单
     */
     @GetMapping(value = "/getTyReConEnterPrise")
     public R getTyReConEnterPrise(@RequestParam String enterpriseCredittype,
                                   @RequestParam String enterpriseIdNumber){
         baseService.logSuccess(3,"查询成功","2");
         return R.ok().put("data",enterpriseService.getTyReConEnterPrise(enterpriseCredittype,enterpriseIdNumber));
     }


    /**
     *添加评价单位或建设单位信用信息黑名单
     */
    @PostMapping(value = "/addReConEnterPrise")
    public R addReConEnterPrise(@RequestBody ApEnterpriseCreditEntity enterPrise) {

        if (StringUtils.isEmpty(enterPrise.getEnterpriseName()) || StringUtils.isEmpty(enterPrise.getEnterpriseIdNumber())
                || StringUtils.isEmpty(enterPrise.getEnterpriseScore()) || StringUtils.isEmpty(enterPrise.getEnterpriseClassification()) || enterPrise.getAssessmentDate() == null
                || StringUtils.isEmpty(enterPrise.getAssessmentAuthority()) || StringUtils.isEmpty(enterPrise.getEnterpriseScoreNumber()) || StringUtils.isEmpty(enterPrise.getSpecause())) {

            return R.error("必填字段为空");
        }
        try{
            String flag = enterpriseService.addReConEnterPrise(enterPrise);
            if (!"0".equals(flag)) {
                R r = new R();
                r.put("code", 200);
                r.put("msg", "操作成功");
                r.ok().put("data", flag);
                baseService.logSuccess(3,"查询成功","2");
                return r;
            } else {
                baseService.logFailed(3,"查询失败","2");
                return R.error("操作失败");
            }
        }catch (Exception e){
            baseService.logFailed(3,"查询失败","2");
            return  R.error(e.getMessage());
        }




    }


    /**
     * 批量删除
     */
    @PostMapping(value = "/deleteEnterPriseList/{enterpriseCredittype}")
    public R deleteExpertCredList(@PathVariable String enterpriseCredittype,@RequestBody String[] enterpriseCreditIds){

        try{
            if(enterpriseCreditIds.length<0 || enterpriseCreditIds==null || StringUtils.isEmpty(enterpriseCredittype)){

                return R.error("操作失败！");
            }
            List<ApEnterpriseCreditEntity> list = new ArrayList();

            for (String enterpriseCreditId : enterpriseCreditIds){

                ApEnterpriseCreditEntity pCred =  enterpriseService.getEnterPrise(enterpriseCreditId,enterpriseCredittype);

                if(pCred!=null){
                    pCred.setDelFlag(Constant.DELETE);//逻辑删除
                    pCred.setUpdateTime(new Date());
                    list.add(pCred);
                }else {
                    return R.error("操作失败！");
                }
            }
            if(list!=null && list.size()>0){
                enterpriseService.update(list);
            }else{
                return R.error("操作失败！");
            }
            baseService.logSuccess(3,"查询成功","2");
            return R.ok();
        }catch (Exception e){
            baseService.logFailed(3,"查询失败","2");
            return R.error(e.getMessage());
        }


    }

    /**
     *  等级认定
     */
    @PostMapping(value = "/updateEnterPriseList")
    public R updateEnterPriseList(@RequestBody List<ApEnterpriseCreditEntity> list ){

     try{

         if(enterpriseService.updateEnterPriseList(list)){
             baseService.logSuccess(3,"查询成功","2");
             return R.ok("操作成功!");
         }else{
             baseService.logFailed(3,"查询失败","2");
             return  R.error("操作失败!");
         }
        }catch (Exception e){
         baseService.logFailed(3,"查询失败","2");
         return R.error(e.getMessage());
      }

    }


}
